home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Reference Guide
/
C-C++ Interactive Reference Guide.iso
/
c_ref
/
csource2
/
sclib_1
/
1_1
/
v7n1070a.txt
< prev
next >
Wrap
Text File
|
1995-11-01
|
3KB
|
195 lines
/* Referencing arrays using array and pointer notation */
#include <stdio.h>
main()
{
static char text[] = "ABCD";
printf("text[0] = %c\n", text[0]);
printf("*(text + 0) = %c\n", *(text + 0));
printf("*(0 + text) = %c\n", *(0 + text));
printf("*text = %c\n", *text);
}
text[0] = A
*(text + 0) = A
*(0 + text) = A
*text = A
/* Referencing pointers using pointer and array notation */
#include <stdio.h>
main()
{
char *ptext = "ABCD";
printf("*ptext = %c\n", *ptext);
printf("*(ptext + 0) = %c\n", *(ptext + 0));
printf("*(0 + ptext) = %c\n", *(0 + ptext));
printf("ptext[0] = %c\n", ptext[0]);
}
*ptext = A
*(ptext + 0) = A
*(0 + ptext) = A
ptext[0] = A
#include <stdio.h>
main()
{
static int i[5] = {1, 2, 3, 4, 5};
int *pi = &i[2];
int j;
for (j = -2; j <= 2; ++j)
printf("pi[%2d] = %d\n", j, pi[j]);
{
char *table[] = {
"red",
"blue",
"green"
};
printf("*table[1] = %c\n", *table[1]);
printf("*(table[1] + 0) = %c\n", *(table[1] + 0));
printf("table[1][0] = %c\n", table[1][0]);
printf("table[2][4] = %c\n", table[2][4]);
}
}
pi[-2] = 1
pi[-1] = 2
pi[ 0] = 3
pi[ 1] = 4
pi[ 2] = 5
*table[1] = b
*(table[1] + 0) = b
table[1][0] = b
table[2][4] = n
#include <stdio.h>
#include <stdlib.h>
main()
{
int *pi;
int i;
pi = malloc(5);
if (pi == NULL) {
/* handle error */
}
for (i = 0; i < 5; ++i) {
pi[i] = i * i;
printf("pi[%d] = %d\n", i, pi[i]);
}
}
pi[0] = 0
pi[1] = 1
pi[2] = 4
pi[3] = 9
pi[4] = 16
#include <stdio.h>
main()
{
char a[10][20];
void f(char [10][20]);
f(a);
}
void f(char a[10][20])
{
printf("sizeof(a) = %lu\n",
(unsigned long) sizeof(a));
printf("sizeof(*a) = %lu\n",
(unsigned long) sizeof(*a));
printf("sizeof(a[0]) = %lu\n",
(unsigned long) sizeof(a[0]));
printf("sizeof(a[0][0]) = %lu\n",
(unsigned long) sizeof(a[0][0]));
}
sizeof(a) = 4
sizeof(*a) = 20
sizeof(a[0]) = 20
sizeof(a[0][0]) = 1
#include <stdio.h>
main()
{
char a[10][20];
char (*b)[20];
void f(char (*)[20]);
b = a;
f(b);
}
void f(char (*a)[20])
{
printf("sizeof(a) = %lu\n",
(unsigned long) sizeof(a));
printf("sizeof(*a) = %lu\n",
(unsigned long) sizeof(*a));
printf("sizeof(a[0]) = %lu\n",
(unsigned long) sizeof(a[0]));
printf("sizeof(a[0][0]) = %lu\n",
(unsigned long) sizeof(a[0][0]));
}
sizeof(a) = 4
sizeof(*a) = 20
sizeof(a[0]) = 20
sizeof(a[0][0]) = 1
#include <stdio.h>
main()
{
char c0;
char c1[1];
char c2[1][1];
char c3[1][1][1];
printf("sizes are = %lu, %lu, %lu, %lu\n",
(unsigned long) sizeof(c0),
(unsigned long) sizeof(c1),
(unsigned long) sizeof(c2),
(unsigned long) sizeof(c3));
}